package cz.muni.fi.pa036.kiss.serviceimpl;

import cz.muni.fi.pa036.kiss.Entity.BilancePlateb;
import cz.muni.fi.pa036.kiss.Entity.BilancePlatebAssembler;
import cz.muni.fi.pa036.kiss.Entity.BilancePlatebDTO;
import cz.muni.fi.pa036.kiss.Entity.StudentAssembler;
import cz.muni.fi.pa036.kiss.Entity.StudentDTO;
import cz.muni.fi.pa036.kiss.dao.BilancePlatebDAO;
import cz.muni.fi.pa036.kiss.service.BilancePlatebService;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author peter
 */
public class BilancePlatebServiceImpl implements BilancePlatebService{

    @Autowired
    private BilancePlatebDAO bilancePlatebDAO;
    @Autowired
    private BilancePlatebAssembler bpassembler;
    @Autowired
    private StudentAssembler studentassembler;
    
    @Transactional
    @Override
    public void create(BilancePlatebDTO bilance) {
        if (bilance == null) {
            throw new IllegalArgumentException("bilance is null");
        }
        
        if (bilance.getId() != null) {
            throw new IllegalArgumentException("bilance.id is not null");
        }
        BilancePlateb bp = bpassembler.createEntityFromDTO(bilance);
        bilancePlatebDAO.create(bp);
        bilance.setId(bp.getId());
    }

    @Transactional(readOnly = true)
    @Override
    public BilancePlatebDTO get(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("bilance.Id is null");
        }
        
        BilancePlateb bp = bilancePlatebDAO.get(id);
        if (bp == null) {
            return null;
        }        
        return bpassembler.createDTOFromEntity(bp);
    }

    @Transactional
    @Override
    public void update(BilancePlatebDTO bilance) {
        if (bilance == null) {
            throw new IllegalArgumentException("bilance is null");
        }        
        if (bilance.getId() == null) {
            throw new IllegalArgumentException("bilance.id is null");
        }
        
        BilancePlateb bp = bpassembler.createEntityFromDTO(bilance);
        bilancePlatebDAO.update(bp);
    }

    @Transactional
    @Override
    public void remove(BilancePlatebDTO bilance) {
        if (bilance == null) {
            throw new IllegalArgumentException("bilance is null");
        }        
        if (bilance.getId() == null) {
            throw new IllegalArgumentException("bilance.id is null");
        }
        BilancePlateb bp = bpassembler.createEntityFromDTO(bilance);
        bilancePlatebDAO.remove(bp);
    }

    @Transactional(readOnly= true)
    @Override
    public List<BilancePlatebDTO> findAll() {
        List<BilancePlateb> result = bilancePlatebDAO.findAll();
        List<BilancePlatebDTO> bpDTOList = new ArrayList();
        
        for (BilancePlateb bp : result) {
            bpDTOList.add(bpassembler.createDTOFromEntity(bp));
        }
        return bpDTOList;
    }

    @Transactional(readOnly= true)
    @Override
    public List<BilancePlatebDTO> findByStudent(StudentDTO student) {
        if (student == null) {
            throw new IllegalArgumentException("student is null");
        }
        List<BilancePlateb> result = bilancePlatebDAO.findByStudent(studentassembler.createEntityfromDTO(student));
        List<BilancePlatebDTO> ret = new ArrayList();
        for (BilancePlateb bp : result) {
            ret.add(bpassembler.createDTOFromEntity(bp));
        }
        return ret;
    }
    
}
